package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.bo.BaseParamBo;
import com.cskaoyan.catmall.model.bo.PromotionCouponBo;
import com.cskaoyan.catmall.model.pojo.MarketCoupon;
import com.cskaoyan.catmall.model.pojo.MarketCouponUser;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.ItemOfCouponListVo;
import com.cskaoyan.catmall.model.vo.PromotionBaseVo;
import com.cskaoyan.catmall.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 创建日期: 2022-01-09-20-09
 * author : LL
 * description: 优惠券相关请求
 */
@RestController
@RequestMapping("admin/coupon")
public class PromotionCouponController {

    @Autowired
    CouponService couponService;
    /**
     * @param 推广管理参数，除基本包含页码及排序的参数外，还包含优惠券种类，状态和名称
     * @return 推广管理返回值，其中列表为优惠券列表
     * @description 推广管理优惠券
     * @author LL
     * @date 2022/1/7
     */
    @RequiresPermissions("admin:coupon:list")
    @GetMapping("list")
    public BaseRespVo getPromotionCoupon(PromotionCouponBo promotionCouponBo) {
        PageHelper.startPage(promotionCouponBo.getPage(), promotionCouponBo.getLimit());
        PromotionBaseVo<ItemOfCouponListVo> promotionBaseVo = couponService.getPromotionCoupons(promotionCouponBo);
        PageInfo<ItemOfCouponListVo> pageInfo = new PageInfo<>(promotionBaseVo.getList());
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        promotionBaseVo.setPages(pages);
        promotionBaseVo.setTotal((int) total);
        promotionBaseVo.setLimit(promotionCouponBo.getLimit());
        promotionBaseVo.setPage(promotionBaseVo.getPage());
        return BaseRespVo.ok(promotionBaseVo);
    }

    /**
     * @param
     * @return
     * @description 添加优惠券
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:coupon:create")
    @PostMapping("create")
    public BaseRespVo createCoupon(@RequestBody MarketCoupon marketCoupon) {
        couponService.createCoupon(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * @param
     * @return
     * @description 优惠券详细信息
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:coupon:read")
    @GetMapping("read")
    public BaseRespVo getCouponInfo(Integer id) {
        MarketCoupon marketCoupon = couponService.getPromotionCouponInfo(id);
        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * @param
     * @return
     * @description 持有对应优惠券的用户列表
     * @author LL
     * @date 2022/1/8
     */
    @GetMapping("listuser")
    public BaseRespVo getCouponUserList(MarketCouponUser couponUser, BaseParamBo baseParamBo) {
        PageHelper.startPage(baseParamBo.getPage(), baseParamBo.getLimit());
        List<MarketCouponUser> couponUsers = couponService.getCouponUsers(couponUser);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(couponUsers);
        PromotionBaseVo<MarketCouponUser> promotionBaseVo = new PromotionBaseVo<>();
        promotionBaseVo.setList(couponUsers);
        promotionBaseVo.setTotal((int) pageInfo.getTotal());
        promotionBaseVo.setPages(pageInfo.getPages());
        promotionBaseVo.setLimit(baseParamBo.getLimit());
        promotionBaseVo.setPage(baseParamBo.getPage());
        return BaseRespVo.ok(promotionBaseVo);

    }

    /**
     * @param
     * @return
     * @description 更新优惠券
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:coupon:update")
    @PostMapping("update")
    public BaseRespVo updateCoupon(@RequestBody MarketCoupon marketCoupon) {
        couponService.updateCoupon(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }


    /**
     * @param
     * @return
     * @description 删除优惠券
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:coupon:delete")
    @PostMapping("delete")
    public BaseRespVo deleteCoupon(@RequestBody MarketCoupon marketCoupon) {
        couponService.deleteCoupon(marketCoupon);
        return BaseRespVo.ok();
    }

}